﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using Computer_Manager.BUS;
using Computer_Manager.DTO;

namespace Computer_Manager.GUI
{
    public partial class frmStatistic : DevExpress.XtraEditors.XtraForm
    {
        BUS_order bus_order = new BUS_order();
        DTO_order dto_order = new DTO_order();
        BUS_order_detail bus_od_de = new BUS_order_detail();
        DTO_order_detail dto_or_de = new DTO_order_detail();
        public frmStatistic()
        {
            InitializeComponent();
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                dateEdit1.Visible = true;
                cmbMonth.Visible = false;
                cmbYear.Visible = false;
                labelControl1.Visible = true;
                labelControl2.Visible = false;
                labelControl3.Visible = false;
            }
            else
            {
                if (radioGroup1.SelectedIndex == 1)
                {
                    dateEdit1.Visible = false;
                    cmbMonth.Visible = true;
                    cmbYear.Visible = true;
                    labelControl1.Visible = false;
                    labelControl2.Visible = true;
                    labelControl3.Visible = true;
                }
                else
                {
                    dateEdit1.Visible = false;
                    cmbMonth.Visible = false;
                    cmbYear.Visible = true;
                    labelControl1.Visible = false;
                    labelControl2.Visible = false;
                    labelControl3.Visible = true;
                }
            }
        }

        private void btnSta_Click(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                dto_order.date_create = Convert.ToDateTime(dateEdit1.Text);
                grcOrder.DataSource = bus_order.GetByDay(dto_order);
                DataTable dt = bus_order.GetByDay(dto_order);
                object sum = dt.Compute("sum(total_money)", "");
                if (sum.ToString() == "")
                {
                    lblDoanhthu.Text = "Doanh thu trong ngày <i>" + dateEdit1.Text + "</i> là: <b> 0 VNĐ </b>";
                    grcOrder.Visible = false;
                }
                else
                {
                    lblDoanhthu.Text = "Doanh thu trong ngày <i>" + dateEdit1.Text + "</i> là: <b>" + String.Format("{0:0,0 vnđ}", sum) + "</b>";
                    grcOrder.Visible = true;
                }
                textEdit.NullValuePrompt = "asdasdasd";
                //ViewProduct.DataSource = bus_order.GetAll();
                lkeDetail.DataSource = bus_od_de.GetAll();
                lkeDetail.DisplayMember = "id_order";
                lkeDetail.ValueMember = "id_order";
            }
            else
            {
                if (radioGroup1.SelectedIndex == 1)
                {
                    string ngay = "01/" + cmbMonth.Text + "/" + cmbYear.Text;
                    DateTime date = Convert.ToDateTime(ngay);
                    dto_order.date_create = date;
                    grcOrder.DataSource = bus_order.GetByMonth(dto_order);
                    DataTable dt = bus_order.GetByMonth(dto_order);
                    object sum = dt.Compute("sum(total_money)", "");
                    if (sum.ToString() == "")
                    {
                        lblDoanhthu.Text = "Doanh thu trong tháng <i>" + cmbMonth.Text+"/" +cmbYear.Text  + "</i> là: <b> 0 VNĐ </b>";
                        grcOrder.Visible = false;
                    }
                    else
                    {
                        lblDoanhthu.Text = "Doanh thu trong tháng <i>" + cmbMonth.Text + "/" + cmbYear.Text + "</i> là: <b>" + String.Format("{0:0,0 vnđ}", sum) + "</b>";
                        grcOrder.Visible = true;
                    }
                    //textEdit.NullValuePrompt = "asdasdasd";
                    lkeDetail.DataSource = bus_od_de.GetAll();
                    lkeDetail.DisplayMember = "id_order";
                    lkeDetail.ValueMember = "id_order";
                    //ViewProduct.DataSource = bus_order.GetAll();
                }
                else
                {
                    string ngay = "01/01/" + cmbYear.Text;
                    DateTime date = Convert.ToDateTime(ngay);
                    dto_order.date_create = date;
                    grcOrder.DataSource = bus_order.GetByYear(dto_order);
                    DataTable dt = bus_order.GetByYear(dto_order);
                    object sum = dt.Compute("sum(total_money)", "");
                    if (sum.ToString() == "")
                    {
                        lblDoanhthu.Text = "Doanh thu trong năm <i>" + cmbYear.Text + "</i> là: <b> 0 VNĐ </b>";
                        grcOrder.Visible = false;
                    }
                    else
                    {
                        lblDoanhthu.Text = "Doanh thu trong năm <i>" + cmbYear.Text + "</i> là: <b>" + String.Format("{0:0,0 vnđ}", sum) + "</b>";
                        grcOrder.Visible = true;
                    }
                    //textEdit.NullValuePrompt = "asdasdasd";
                    lkeDetail.DataSource = bus_od_de.GetAll();
                    lkeDetail.DisplayMember = "id_order";
                    lkeDetail.ValueMember = "id_order";
                    //ViewProduct.DataSource = bus_order.GetAll();
                }
            }
        }

        private void frmStatistic_Load(object sender, EventArgs e)
        {
            dateEdit1.Visible = true;
            dateEdit1.DateTime = DateTime.Now;
            cmbMonth.Visible = false;
            cmbYear.Visible = false;
            labelControl1.Visible = true;
            labelControl2.Visible = false;
            labelControl3.Visible = false;
            DateTime date = DateTime.Now;
            for (int i = date.Year - 50; i <= date.Year; i++)
            {
                cmbYear.Items.Add(i);
            }
            for (int i = 1; i <= 12; i++)
            {
                cmbMonth.Items.Add(i);
            }
            cmbYear.SelectedItem = date.Year;
            cmbMonth.SelectedItem = date.Month;
            //ViewProduct.DataSource = bus_order.GetAll();
            lkeDetail.DataSource = bus_od_de.GetAll();
            lkeDetail.DisplayMember = "id_order";
            lkeDetail.ValueMember = "id_order";
        }

    }
}